@model IPageResponseData
@inject IAdminRouteLibrary adminRouteLibrary
@inject IPermissionValidationService permissionValidationService
@inject ICofoundryHelper Cofoundry
@inject IPageRouteLibrary pageRouteLibrary
@inject ImageAssetsSettings imageAssetsSettings

@functions {
    bool IsEditTypeSwitchRequired(IPageResponseData pageResponseData)
    {
        return pageResponseData.PageRoutingInfo.CustomEntityRoute != null
            && pageResponseData.PageVersion.HasCustomEntityRegions
            && pageResponseData.PageVersion.HasPageRegions;
    }

    bool IsCustomEntityEditOnly(IPageResponseData pageResponseData)
    {
        return pageResponseData.PageRoutingInfo.CustomEntityRoute != null
            && pageResponseData.PageVersion.HasCustomEntityRegions
            && !pageResponseData.PageVersion.HasPageRegions;
    }
}

@{
    var pageResponseData = Model;
    var isStaticPage = pageResponseData == null;
    var hasPageEditPermission = false;
    var hasPagePublishPermission = false;
    var hasCustomEntityEditPermission = false;
    var hasCustomEntityPublishPermission = false;
    var canEditDraft = false;

    string currentVersion = string.Empty;
    bool isEditing = false;
    bool isViewingSpecificVersion = false;
    bool isCustomEntityRoute = false;
    bool isEditTypeSwitchRequired = false;
    bool isCustomEntityEditOnly = false;
    string entityNameSingular = string.Empty;
    string backToAdminLink = string.Empty;
    IEnumerable<IVersionRoute> versions = null;

    string iFrameParams = string.Empty;
    string rootUrlPath = Context.Request.Path;
    PublishState? publishState = null;

    if (!isStaticPage)
    {
        hasPageEditPermission = permissionValidationService.HasPermission<PageUpdatePermission>(pageResponseData.CofoundryAdminUserContext);
        hasPagePublishPermission = permissionValidationService.HasPermission<PagePublishPermission>(pageResponseData.CofoundryAdminUserContext);
        isEditing = pageResponseData.VisualEditorMode == VisualEditorMode.Edit;
        isViewingSpecificVersion = pageResponseData.VisualEditorMode == VisualEditorMode.SpecificVersion;
        isCustomEntityRoute = pageResponseData.Version is CustomEntityVersionRoute;
        publishState = pageResponseData.PageRoutingInfo.GetPublishState();

        var customEntityDefinitionCode = pageResponseData.PageRoutingInfo.PageRoute.CustomEntityDefinitionCode;
        if (!string.IsNullOrEmpty(customEntityDefinitionCode))
        {
            hasCustomEntityEditPermission = permissionValidationService.HasCustomEntityPermission<CustomEntityUpdatePermission>(customEntityDefinitionCode, pageResponseData.CofoundryAdminUserContext);
            hasCustomEntityPublishPermission = permissionValidationService.HasCustomEntityPermission<CustomEntityPublishPermission>(customEntityDefinitionCode, pageResponseData.CofoundryAdminUserContext);
        }

        entityNameSingular = isCustomEntityRoute ? pageResponseData.CustomEntityDefinition.Name : "Page";
        isEditTypeSwitchRequired = IsEditTypeSwitchRequired(pageResponseData) && hasCustomEntityEditPermission && hasPageEditPermission;
        isCustomEntityEditOnly = IsCustomEntityEditOnly(pageResponseData) && hasCustomEntityEditPermission;
        canEditDraft = (pageResponseData.PageVersion.HasCustomEntityRegions || pageResponseData.PageVersion.HasPageRegions)
            && (hasPageEditPermission || hasCustomEntityEditPermission);
        // If we are on a page with custom entities, it makes more sense to refer to the page as a template
        if (isEditTypeSwitchRequired && !isCustomEntityRoute)
        {
            entityNameSingular = "Template";
        }


        iFrameParams = string.Format("?isInEditMode={0}&pageId={1}&versionId={2}&isCustomEntityRoute={3}&entityNameSingular={4}&entityId={5}&hasDraftVersion={6}",
            isEditing.ToString(),
            pageResponseData.PageRoutingInfo.PageRoute.PageId.ToString(),
            pageResponseData.Version.VersionId.ToString(),
            isCustomEntityRoute.ToString(),
            entityNameSingular.ToString(),
            (isCustomEntityRoute ? pageResponseData.PageRoutingInfo.CustomEntityRoute.CustomEntityId : pageResponseData.PageRoutingInfo.PageRoute.PageId).ToString(),
            pageResponseData.HasDraftVersion.ToString());

        backToAdminLink = adminRouteLibrary.Dashboard.Dashboard();

        if (isCustomEntityRoute)
        {
            backToAdminLink = adminRouteLibrary.CustomEntities.Details(pageResponseData.CustomEntityDefinition, pageResponseData.PageRoutingInfo.CustomEntityRoute.CustomEntityId);
            versions = pageResponseData.PageRoutingInfo.CustomEntityRoute.Versions;
        }
        else
        {
            versions = pageResponseData.PageRoutingInfo.PageRoute.Versions;
        }
    }
}

<!-- ADMIN IFRAME -->
<iframe 
    id="cofoundry-src__iFrame" 
    src="@adminRouteLibrary.VisualEditor.MvcRoute("frame", iFrameParams)"
    class="cofoundry-sv__iFrame">
</iframe>
<!-- END ADMIN IFRAME -->

<!-- PAGE TOOLBAR -->
<div id="cofoundry-sv" class="cofoundry-sv active">

    <div class="cofoundry-sv__navmenu">
        <div class="cofoundry-sv__mini-menu cofoundry-sv__mini-menu--align-left">            
            @* No cofoundry user available on static pages at the moment, so disable these links for now*@
            @if (!isStaticPage)
            {
            <div class="cofoundry-sv__mini-menu__content">
                <ol class="cofoundry-sv__navmenu-list">
@if (permissionValidationService.HasPermission<DashboardAdminModulePermission>(pageResponseData.CofoundryAdminUserContext))
{
                    <li>
                        <a href="@adminRouteLibrary.Dashboard.Dashboard()" class="cofoundry-sv__navmenu-list__item">
                            <span class="fa fa-chevron-left"></span>
                            Dashboard
                        </a>
                    </li>
}

@if (permissionValidationService.HasPermission<PageAdminModulePermission>(pageResponseData.CofoundryAdminUserContext))
{
                    <li>
                        <a href="@adminRouteLibrary.Pages.List()" class="cofoundry-sv__navmenu-list__item">
                            <span class="fa fa-chevron-left"></span>
                            Pages
                        </a>
                    </li>
}
@if (permissionValidationService.HasPermission<PageDirectoryAdminModulePermission>(pageResponseData.CofoundryAdminUserContext))
{
                    <li>
                        <a href="@adminRouteLibrary.Directories.List()" class="cofoundry-sv__navmenu-list__item">
                            <span class="fa fa-chevron-left"></span>
                            Directories
                        </a>
                    </li>
}
@if (!imageAssetsSettings.Disabled && permissionValidationService.HasPermission<ImageAssetAdminModulePermission>(pageResponseData.CofoundryAdminUserContext))
{
                    <li>
                        <a href="@adminRouteLibrary.Images.List()" class="cofoundry-sv__navmenu-list__item">
                            <span class="fa fa-chevron-left"></span>
                            Images
                        </a>
                    </li>
}
@if (permissionValidationService.HasPermission<SettingsAdminModulePermission>(pageResponseData.CofoundryAdminUserContext))
{
                    <li>
                        <a href="@adminRouteLibrary.Settings.Details()" class="cofoundry-sv__navmenu-list__item">
                            <span class="fa fa-chevron-left"></span>
                            Settings
                        </a>
                    </li>
}
                    @if (!isStaticPage && canEditDraft && !isViewingSpecificVersion && isCustomEntityRoute)
                    {
                        <li class="cofoundry-sv__navmenu-list__divider">
                            <a href="@backToAdminLink" class="cofoundry-sv__navmenu-list__item">
                                <span class="fa fa-pencil"></span>
                                Edit this @pageResponseData.CustomEntityDefinition.Name in admin suite
                            </a>
                        </li>
                    }
                </ol>
            </div>
            }
            <a href="@adminRouteLibrary.Dashboard.Dashboard()" class="cofoundry-sv__mini-menu__toggle">
                <img class="cofoundry-sv__logo" src="@adminRouteLibrary.VisualEditor.StaticResource("img/logo-thumb.png")" />
            </a>
        </div>
    </div>

    <div class="cofoundry-sv__page-details">
        <div class="cofoundry-sv__mini-menu cofoundry-sv__mini-menu--flow">
            @if (!isStaticPage && versions.Any())
            {
                int versionCount = versions.Count();

                <div class="cofoundry-sv__mini-menu__content">
                    <h3>@entityNameSingular Versions</h3>
                    <ol class="cofoundry-sv__version-list">
                        @foreach (var version in versions.Take(8))
                        {
                            var link = pageRouteLibrary.VisualEditor(pageResponseData.PageRoutingInfo, version);
                            bool isActive = false;
                            string versionText;

                            if (version.WorkFlowStatus == WorkFlowStatus.Draft)
                            {
                                versionText = "Draft";
                            }
                            else
                            {
                                versionText = "V" + versionCount;
                            }

                            if (version.IsLatestPublishedVersion)
                            {
                                versionText += " (" + TextFormatter.PascalCaseToSentence(publishState.Value.Description.ToString()) + ")";
                            }

                            if (version == pageResponseData.Version)
                            {
                                isActive = true;
                                currentVersion = versionText;
                            }

                            versionCount--;

                            <li>
                                <a href="@link" class="cofoundry-sv__version-list__item @Cofoundry.Html.TextIf(isActive, "active")">
                                    <span class="@version.WorkFlowStatus.ToString().ToLower()"></span>
                                    @versionText, Created on @version.CreateDate
                                </a>
                            </li>
                        }
                    </ol>

                </div>
            }
            @if (!isStaticPage)
            {
                <button type="button" class="cofoundry-sv__mini-menu__toggle">
                    <span class="cofoundry-sv__page-details__status @pageResponseData.Version.WorkFlowStatus.ToString().ToLower()"></span>
                    <span class="cofoundry-sv__page-details__name">@currentVersion, @pageResponseData.Version.Title</span>
                    @if (Context.Request.Path.Value.Length > 1)
                    {
                        <span class="cofoundry-sv__page-details__url">@Context.Request.Path</span>
                    }
                </button>
            }
            else
            {
                <button class="cofoundry-sv__mini-menu__toggle">
                    <span class="cofoundry-sv__page-details__name">@Context.Request.Path.Value</span>
                    <span class="cofoundry-sv__page-details__url">Static page</span>
                </button>
            }
        </div>
    </div>
    <div class="cofoundry-sv__mode">
        @if (!isStaticPage && !isViewingSpecificVersion)
        {
            if (publishState.Value.Description == PublishStateDescription.Published)
            {
            <a href="@pageRouteLibrary.VisualEditor(pageResponseData.PageRoutingInfo, VisualEditorMode.Live, isCustomEntityRoute)" class="cofoundry-sv__mode__button @Cofoundry.Html.TextIf(pageResponseData.Version.WorkFlowStatus == WorkFlowStatus.Published, "active")">
                <span>
                    @*<svg class="icon" viewBox="0 0 80 80">
                        <use xlink:href="#currentlive" x="0" y="0" />
                    </svg>*@
                    Current<br />live
                </span>
            </a>
            }

            if (canEditDraft)
            {
                if (isEditTypeSwitchRequired)
                {
                    <span class="cofoundry-sv__mode__button__group">
                        <a href="@pageRouteLibrary.VisualEditor(pageResponseData.PageRoutingInfo, VisualEditorMode.Edit, true)" class="cofoundry-sv__mode__button @Cofoundry.Html.TextIf(isCustomEntityRoute && isEditing, "active")">
                            <span>
                                @*<svg class="icon" viewBox="0 0 80 80">
                                    <use xlink:href="#editentity" x="0" y="0" />
                                </svg>*@
                                Edit<br />
                                @pageResponseData.CustomEntityDefinition.Name
                            </span>
                        </a>
                        <a href="@pageRouteLibrary.VisualEditor(pageResponseData.PageRoutingInfo, VisualEditorMode.Edit, false)" class="cofoundry-sv__mode__button @Cofoundry.Html.TextIf(!isCustomEntityRoute && isEditing, "active")">
                            <span>
                                @*<svg class="icon" viewBox="0 0 80 80">
                                    <use xlink:href="#edittemplate" x="0" y="0" />
                                </svg>*@
                                Edit<br />
                                template
                            </span>
                        </a>
                    </span>
                }
                else
                {
                    <a href="@pageRouteLibrary.VisualEditor(pageResponseData.PageRoutingInfo, VisualEditorMode.Edit, isCustomEntityRoute)" class="cofoundry-sv__mode__button @Cofoundry.Html.TextIf(isEditing, "active")">
                        <span>
                            @*<svg class="icon" viewBox="0 0 80 80">
                                <use xlink:href="#edittemplate" x="0" y="0" />
                            </svg>*@
                            Edit<br />draft
                        </span>
                    </a>
                }
                @if (pageResponseData.HasDraftVersion)
                {
                <a href="@pageRouteLibrary.VisualEditor(pageResponseData.PageRoutingInfo, VisualEditorMode.Preview, isCustomEntityRoute)" class="cofoundry-sv__mode__button @Cofoundry.Html.TextIf(!isEditing && pageResponseData.Version.WorkFlowStatus == WorkFlowStatus.Draft, "active")">
                    <span>
                        @*<svg class="icon" viewBox="0 0 80 80">
                            <use xlink:href="#preview" x="0" y="0" />
                        </svg>*@
                        Preview<br />draft
                    </span>
                </a>
                }
            }
        }
        <div class="cofoundry-sv__options">

        </div>
    </div>
</div>
<!-- END PAGE TOOLBAR -->

@if (!isStaticPage && isEditing)
{
<!-- REGION TOOLBAR UI -->
<button
    id="cofoundry-sv__btn-add-block"
    class="cofoundry-sv__btn-add-block"
    type="button"
    style="display:none;">
</button>

<!-- END REGION TOOLBAR UI -->

<!-- BLOCK TOOLBAR UI -->
<div id="cofoundry-sv__block-popover-container">
    <ul class="cofoundry-sv__block-popover--main-actions">
        <li class="edit">
            <button id="cofoundry-sv__btn-block-edit"
                    type="button"
                    title="Edit this block">
                <i class="fa fa-edit"></i> Edit
            </button>
        </li>
        <li class="advanced">
            <button type="button">...</button>
            <div class="cofoundry-sv__block-popover--adv-actions">
                
                <ul>
                    <li class="up">
                        <button id="cofoundry-sv__btn-block-moveup"
                                type="button"
                                title="Move this content block up">
                            <i class="fa fa-chevron-up"></i> Move up
                        </button>
                    </li>
                    <li class="down">
                        <button id="cofoundry-sv__btn-block-movedown"
                                type="button"
                                title="Move this content block down">
                            <i class="fa fa-chevron-down"></i> Move down
                        </button>
                    </li>
                    <li class="addAbove">
                        <button id="cofoundry-sv__btn-block-addabove"
                                type="button"
                                title="Add a new content block above">
                            <i class="fa fa-chevron-circle-up"></i> Add above
                        </button>
                    </li>
                    <li class="addBelow">
                        <button id="cofoundry-sv__btn-block-addbelow"
                                type="button"
                                title="Add a new content block below">
                            <i class="fa fa-chevron-circle-down"></i> Add below
                        </button>
                    </li>
                    <li class="delete">
                        <button id="cofoundry-sv__btn-block-delete"
                                type="button"
                                title="Delete this content block">
                            <i class="fa fa-trash-o"></i> Delete
                        </button>
                    </li>
                </ul>
            </div>
        </li>
    </ul>
</div>
<!-- END BLOCK TOOLBAR UI -->

<div id="cofoundry-sv__ui-wrap" style="display:none;">
    <div class="cofoundry-sv__ui-wrap-name">Placeholder</div>
</div>
}